package service;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.DateKits;
import common.kit.PageSql;
import model.EquipmentTypes;
import model.MonitoringFactor;
import model.Staff;
import model.Station;

public class MonitoringFactorService extends BaseService {

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tmf.stationId,tmf.id,tmf.factor,tmf.range,tmf.limit,tmf.acceptanceTime,tmf.networkingTime,tsf.name,tsr.name AS servicerName " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_monitoring_factor tmf ") ;
	    pageSql.join(" t_station ts " , " ts.id = tmf.stationId ") ;
	    pageSql.join(" t_staff tsf " , " tsf.id = ts.staffId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tsf.servicerId ") ;
		pageSql.setEqualCondition("tmf.stationId") ; //点位	 
	    pageSql.orderBy(" tmf.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		List<Record> list = page.getList();
		for (Record record : list) {
			record.set("acceptanceTime", DateKits.toStr(record.getDate("acceptanceTime")));		
			record.set("networkingTime", DateKits.toStr(record.getDate("networkingTime")));		
		}
		return page ;
	}

	public Ret save(MonitoringFactor monitoringFactor , Station station , Staff staff ) {
		monitoringFactor.setStationId(station.getId());
		monitoringFactor.save();
		station.setStaffId(staff.getId());
		station.update();
		EquipmentTypes.dao.save(station.getId(), monitoringFactor.getFactor());
	    return ok() ;
	}
	
	public Ret delete(Long id) {
		MonitoringFactor monitoringFactor = MonitoringFactor.dao.findById(id);
		if (null == monitoringFactor) {
			return fail();
		}
		monitoringFactor.delete() ;
		return ok() ;
	}
}
